//
//  MainViewController.m
//  QQchat
//
//  Created by cocoazxj@163.com on 15/3/29.
//  Copyright (c) 2015年 handcool. All rights reserved.
//

#import "MainViewController.h"
#import "CustomTableViewCell.h"
#import "ChatModel.h"

@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
@property (nonatomic, strong) NSMutableArray *mArray;
@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (NSMutableArray *)mArray
{
    if (!_mArray)
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"messages" ofType:@".plist"];
        NSArray *a = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *ma = [NSMutableArray array];
        
        for(NSDictionary *d in a)
        {
            ChatModel *chat = [ChatModel chatModel:d];
            ChatModel *m = [ma lastObject];
            NSLog(@"------%@-------%@",chat.time,m.time);
            if ([m.time isEqualToString:chat.time]) {
                NSLog(@"YES");
                chat.isTime = YES;
            };
            [ma addObject:chat];
        }
        self.mArray = ma;
    }
    return _mArray;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIView *v =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, self.tf_text.frame.size.height)];
    self.tf_text.leftView = v;
    self.tf_text.leftViewMode = UITextFieldViewModeAlways;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cancelKey:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    // 注册监听通知方法
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(canThing:) name:@"name" object:nil];
    
}

- (void)cancelKey:(NSNotification *)notification
{
    NSDictionary *d = notification.userInfo;
    CGRect r = [d[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSLog(@"-------------%f",self.view.frame.size.height);
    self.view.transform = CGAffineTransformMakeTranslation(0, r.origin.y - self.view.frame.size.height);
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -
#pragma mark UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChatModel *m = self.mArray[indexPath.row];
    return m.cellH;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"identifier";
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(!cell)
    {
        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.chatmodel = [self.mArray objectAtIndex:indexPath.row];
    return cell;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"--------%@",textField.text);
    ChatModel *m = [[ChatModel alloc] init];
    m.time = @"今天 16：50";
    m.text = textField.text;
    m.type = 1;
    [self.mArray addObject:m];
    [self.tv_main reloadData];
    
    NSIndexPath *index = [NSIndexPath indexPathForRow:self.mArray.count - 1 inSection:0];
    [self.tv_main scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    textField.text = nil;
    return YES;
}

-(BOOL)prefersStatusBarHidden
{
    return YES;
}

- (IBAction)testButton:(id)sender
{
    NSLog(@"===========");
    NSDictionary *d = @{@"test":@"Hello Notification"};
    // 发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"name" object:self userInfo:d];
}
- (void)canThing:(NSNotification *)notification
{
    NSLog(@"------%@----",notification.name);
    NSLog(@"------%@----",notification.userInfo);
}
@end
